# -*- coding: utf-8 -*-
# filename: udp_server.py

import sys
import socket

def writeGps(data):
    with open("gps.data", "wb") as fs:
        fs.write(data)


def udp_server(port='8080'):
    udp_addr = ('0.0.0.0', int(port))
    #udp_addr = ('172.28.171.87', int(port))
    udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    udps.bind(udp_addr)
    
    while True:
        try:
            udp_data, udpAddr  = udps.recvfrom(255)
            print "OK: ", udp_data, len(udp_data)
            if len(udp_data) == 0:
                udps.close()
            else:
                writeGps(udp_data)
        except Exception as e:
            print e
            udps.close()

if __name__ == '__main__':
    if len(sys.argv) == 2:
        print sys.argv[1]
        udp_server(sys.argv[1])
    else:
        udp_server()
